package atguigu2.lock;

import java.util.concurrent.TimeUnit;

/**
 * @AUTHOR LYF
 * @DATE 2022/2/3
 * @VERSION 1.0
 * @DESC
 * jps,jstack进行发现死锁
 */
public class DeadLock {


    public static void main(String[] args) {

        Object o1 = new Object();
        Object o2 = new Object();
        new Thread(()->{
            synchronized (o1){
                System.out.println(Thread.currentThread().getName()+"持有锁 A;想获取锁B");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (o2){
                    System.out.println(Thread.currentThread().getName()+"获取锁B");
                }
            }
        },"AA").start();

        new Thread(()->{
            synchronized (o2){
                System.out.println(Thread.currentThread().getName()+"持有锁 A;想获取锁B");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (o1){
                    System.out.println(Thread.currentThread().getName()+"获取锁B");
                }
            }
        },"BB").start();
    }



}
